const util = require('../../../../utils/util.js')
const app = getApp()

// 实例化API核心类
const qqmapsdk = app.globalData.qqmapsdk

Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: '',
    poi: {
      latitude: '',
      longitude: ''
    },
    addressName: '',
    time: '',
    timer: '',
    timer2: '', // 用来每个一段时间自动刷新一次定位

    lessonid: '',
    teaAddress: '',
    teaLocation: ''
  },

  getAddress(e) {
    var that = this;
    qqmapsdk.reverseGeocoder({
      //位置坐标，默认获取当前位置，非必须参数
      /**
       * 
        location: {
          latitude: 39.984060,
          longitude: 116.307520
        },
      */
      // 成功后的回调
      success: function (res) {
        // console.log(res);
        that.setData({
          addressName: res.result.address
        })
        var res = res.result;
        var mks = [];
        //当get_poi为0时或者为不填默认值时，检索目标位置，按需使用
        mks.push({ // 获取返回结果，放到mks数组中
          title: res.address,
          id: 0,
          latitude: res.location.lat,
          longitude: res.location.lng,
          iconPath: '/images/map/myPosition.png', // 图标路径
          width: 21,
          height: 28,
          // callout: { //在markers上展示地址名称，根据需求是否需要
          //   content: res.address,
          //   color: '#000',
          //   display: 'ALWAYS'
          // }
        });
        that.setData({ // 设置markers属性和地图位置poi，将结果在地图展示
          markers: mks,
          poi: {
            latitude: res.location.lat,
            longitude: res.location.lng
          }
        });
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    })
  },
  getTime: function () {
    let that = this
    let time = that.data.time
    that.setData({
      timer: setInterval(function () {
        time = util.formatTime(new Date())
        that.setData({
          time: time.substr(-8)
        });
        if (time == 0) {
          // 页面跳转后，要把定时器清空掉，免得浪费性能
          clearInterval(that.data.timer)
        }
      }, 1000)
    })
  },
  rePosition: function () {
    console.log('用户点了重新定位')
    this.getAddress()
  },
  checkIn: function () {
    console.log('用户点击了签到')

    var that = this
    var nowTime = util.formatTime(new Date())
    wx.showModal({
      title: '请确认发布考勤信息',
      // content: '',
      content: `地点：${this.data.addressName}\n时间：${nowTime}`, 
      confirmText: '确认',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          // 调起签到接口
          that.teamap()

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  teamap: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.ServerUrl + 'map/teamap.php',
      data: {
        id: this.data.lessonid,
        teaAddress: this.data.addressName,
        teaLocation: this.data.poi.latitude + ',' + this.data.poi.longitude
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data) {
          wx.showToast({
            title: '发起考勤成功',
            icon: 'none'
          })
          wx.navigateTo({
            url: '../signByMapList/signByMapList?lessonid=' + that.data.lessonid,
          })
        } else {
          wx.showToast({
            title: '发起考勤失败',
            icon: 'none'
          })
        }
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getTime()
    that.getAddress()

    that.setData({
      timer2: setInterval(function () {
        that.getAddress()
      }, 200000), // 每20秒刷新一次定位
      lessonid: options.lessonid
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer)
    clearInterval(this.data.timer2)
    console.log("定时器已被清除")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})